/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lojavirtual.persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.RowId;
import java.sql.Statement;
import java.util.List;
import lojavirtual.model.Usuario;
import lojavirtual.util.ConnectionFactory;

/**
 *
 * @author 710930
 */
public class UsuarioDAO {

    public boolean inserir(Usuario usuario) {
    Connection connection = ConnectionFactory.getConnection();

       try{

           String SQL = " INSERT INTO USUARIO(LOGIN, SENHA, NOME, CARTAO) VALUES (?, ?, ?, ?) ";
           PreparedStatement statement = connection.prepareStatement(SQL);
           statement.setString(1, usuario.getLogin());
           statement.setString(2, usuario.getSenha());
           statement.setString(3, usuario.getNome());
           statement.setString(4, usuario.getCartao());
           statement.executeUpdate();

           return true;
       } catch (Exception e){ 
           e.printStackTrace();
           return false;
       }
       finally { ConnectionFactory.close(connection); }
    }

    public void alterar(Usuario usuario) {
       Connection connection = ConnectionFactory.getConnection();
       
       try{

           String SQL = "UPDATE USUARIO (SENHA, NOME, CARTAO) SET (?, ?, ?) WHERE LOGIN = ?";

           PreparedStatement statement = connection.prepareStatement(SQL);
           statement.setString(1, usuario.getSenha());
           statement.setString(2, usuario.getNome());
           statement.setString(3, usuario.getCartao());
           statement.setString(4, usuario.getLogin());
           statement.executeUpdate();

       } catch (Exception e){ e.printStackTrace();}
       finally { ConnectionFactory.close(connection); }
    }

    public List<Usuario> listar(Usuario usuario) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void remover(Usuario usuario) {
        Connection connection = ConnectionFactory.getConnection();

       try{

           String SQL = "DELETE FROM USUARIO WHERE LOGIN = ?";

           PreparedStatement statement = connection.prepareStatement(SQL);
           statement.setString(1, usuario.getLogin());
           statement.executeUpdate();

       } catch (Exception e){ e.printStackTrace();}
       finally { ConnectionFactory.close(connection); }
    }

    public boolean logar(Usuario usuario){
       Connection connection = ConnectionFactory.getConnection();

       try{

           String SQL = " SELECT NOME FROM USUARIO WHERE LOGIN = '" + usuario.getLogin()+"' AND senha= '" + usuario.getSenha() +"'";

           Statement statement = connection.createStatement();
           ResultSet rs = statement.executeQuery(SQL);
           rs.next();

           String valor = rs.getString("NOME");

           if(valor == null) return false;
           else return true;

       } catch (Exception e){ e.printStackTrace();}
       finally { ConnectionFactory.close(connection); }

       return false;
       
    }
}
